package com.pospi.pospihold.ui.login_ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.pospi.pospihold.R;
import com.pospi.pospihold.adapter.StoreSelectionAdapter;
import com.pospi.pospihold.dao.GoodsDao;
import com.pospi.pospihold.dto.ReturnDto;
import com.pospi.pospihold.dto.StoreMsgDto;
import com.pospi.pospihold.http.LoadServer;
import com.pospi.pospihold.ui.base.BaseActivity;
import com.pospi.pospihold.util.Api;
import com.pospi.pospihold.util.Constant;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SelectStoreActivity extends BaseActivity {

    @Bind(R.id.lv_store)
    ListView lvStore;

    private List<StoreMsgDto> msgDtos = new ArrayList<>();
    private StoreSelectionAdapter adapter;
    private GoodsDao goodsDao;

    private String shopId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_select_store);
        ButterKnife.bind(this);

        setTitle("店铺选择");
        setBackVisible(false);
        downLoadStoreMsg();

        lvStore.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StoreMsgDto msgDto = msgDtos.get(position);
                SharedPreferences.Editor editor = getSharedPreferences("StoreMessage", MODE_PRIVATE).edit();
                editor.putString("Id", msgDto.getId());
                editor.putString("Name", msgDto.getName());
                editor.putString("Address", msgDto.getAddress());
                editor.putString("Contacts", msgDto.getContacts());
                editor.putString("Phone", msgDto.getPhone());
                editor.apply();
                SPHelper.getInstance().putString(Constant.SHOP_ID, msgDto.getId());
                shopId = msgDto.getId();


                startActivity(CashierLoginActivity.class);
                finish();
            }
        });

    }


    public void downLoadStoreMsg() {
        RequestParams params = new RequestParams();
        LoadServer.getConnect(getApplicationContext(), Api.SYNCSHOP, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int position, Header[] headers, byte[] bytes) {
                Log.i("responseBody", new String(bytes));
                ReturnDto returnDto = new ReturnDto();
                try {
                    JSONObject object = new JSONObject(new String(bytes));
                    returnDto.setResult(object.getInt("Result"));
                    returnDto.setMessage(object.getString("Message"));
                    JSONArray array = object.getJSONArray("Value");
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = (JSONObject) array.opt(i);
                        StoreMsgDto msgDto = new StoreMsgDto();
                        msgDto.setId(object1.getString("Id"));
                        msgDto.setName(object1.getString("Name"));
                        msgDto.setAddress(object1.getString("Address"));
                        msgDto.setPhone(object1.getString("Phone"));
                        msgDto.setContacts(object1.getString("Contacts"));
                        msgDtos.add(msgDto);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (returnDto.getResult() == 1) {
                    //解析value得到店铺的信息
                    adapter = new StoreSelectionAdapter(msgDtos, getApplicationContext());
                    lvStore.setAdapter(adapter);
                } else {
                    showToast(returnDto.getMessage());
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }

}
